//
//  ChatAdvisoruDetailView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/20.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class ChatAdvisoruDetailView: UIView {
    
    var imageClickedClosure: ((_ imageUrls: [String], _ index: Int) -> Void)?
    
    @IBOutlet private weak var nameLabel: UILabel!
    @IBOutlet private weak var allergyHistoryLabel: UILabel! {
        didSet {
            allergyHistoryLabel.isHidden = true
        }
    }
    @IBOutlet private weak var ahTitleLabel: UILabel! {
        didSet {
            ahTitleLabel.isHidden = true
        }
    }
    @IBOutlet private weak var phTitleLabel: UILabel! {
        didSet {
            phTitleLabel.isHidden = true
        }
    }
    @IBOutlet private weak var pastHistoryLabel: UILabel! {
        didSet {
            pastHistoryLabel.isHidden = true
        }
    }
    @IBOutlet private weak var descLabel: UILabel!
    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.isHidden = true
            collectionView.showsVerticalScrollIndicator = false
            collectionView.showsHorizontalScrollIndicator = false
            collectionView.delaysContentTouches = false
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(ChatDetailImageCell.nibForCell(),
                                    forCellWithReuseIdentifier: ChatDetailImageCell.reuseIdentifier())
        }
    }
    
    @IBOutlet private weak var expandBtn: UIButton! {
        didSet {
            expandBtn.isHidden = true
        }
    }
    
    @IBOutlet private weak var collectionViewHContraint: NSLayoutConstraint!
    
    private var viewModel: AdvisoryDetailViewModel?
    
    private var imageUrls = [String]()
    
    func configure(with vm: AdvisoryDetailViewModel) {
        viewModel = vm
        descLabel.text = vm.conditionDesc
        collectionView.reloadData()
        nameLabel.text = String(format: "%@  %@  %@  %@", vm.patientName, vm.patientSex, vm.patientAge, vm.cardText)
        allergyHistoryLabel.text = vm.allergyHistory.isEmpty ? "无" : vm.allergyHistory
        pastHistoryLabel.text = vm.pastHistory.isEmpty ? "无" : vm.pastHistory
        
        imageUrls = vm.detailImages.map({ model -> String in
            return model.url ?? ""
        })
        
        layoutIfNeeded()
        self.frame = CGRect(x: 0, y: navBarH, width: screenW, height: vm.viewHeight())
        collectionViewHContraint.constant = vm.collectionViewH()
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear, animations: {
            self.layoutIfNeeded()
        }) { _ in
            self.hiddenSubView(false)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
    }
    
    @IBAction private func expandBtnClicked(_ sender: UIButton) {
        guard let vm = viewModel else { return }
        sender.isSelected.toggle()
        
        layoutIfNeeded()
        self.frame = CGRect(x: 0, y: navBarH, width: screenW, height: vm.viewHeight(isExpand: sender.isSelected))
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear, animations: {
            self.layoutIfNeeded()
            self.hiddenSubView(sender.isSelected)
        })
    }
    
    private func hiddenSubView(_ isExpand: Bool) {
        collectionView.isHidden = false
        expandBtn.isHidden = false
        descLabel.numberOfLines = !isExpand ? 1 : 0
        ahTitleLabel.isHidden = !isExpand
        phTitleLabel.isHidden = !isExpand
        allergyHistoryLabel.isHidden = !isExpand
        pastHistoryLabel.isHidden = !isExpand
    }
}

extension ChatAdvisoruDetailView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.numberOfItems ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChatDetailImageCell.reuseIdentifier(), for: indexPath) as! ChatDetailImageCell
        if let vm = viewModel?.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
}

extension ChatAdvisoruDetailView: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemWH = viewModel?.itemWH ?? 0
        return CGSize(width: itemWH, height: itemWH)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        imageClickedClosure?(imageUrls, indexPath.item)
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? PublishPhotoCell
        cell?.backgroundColor = .splLineNormal
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? PublishPhotoCell
        cell?.backgroundColor = .clear
    }
}
